package org.gwtEss.menu;

import java.util.List;

import org.gwtEss.table.ColWidget;
import org.gwtEss.table.Cols;
import org.gwtEss.table.ReadOnlyTextColWidget;
import org.gwtEss.table.TabAdapter;
import org.gwtEss.table.TextDesc;
import org.gwtEss.table.UserManagedColWidget;
import org.gwtEss.table.adapter.SmartUserManagedImageColWidget;

import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
public class MenuTable extends TabAdapter<MenuItemBase> {
	public MenuTable() {
		super(new Cols<MenuItemBase>(){
			{
				
				add(new UserManagedColWidget<MenuItemBase, Widget>("", 20) {

					@Override
					public Widget createColWidgetInstance(MenuItemBase data) {
						if (data instanceof HasMenuItemSeperator)
							return new HTML("<HR width='100%'/>");
						if (data instanceof HasMenuItemTitle)
							return new HTML("<b>" + ((HasMenuItemTitle)data).getTitleText() + "</b>");
						Widget iconWidget = null; 
						if (data instanceof HasMenuItemIcon) iconWidget = ((HasMenuItemIcon)data).getIcon();
//						HasMenuItem menuItem = (HasMenuItem)data;
						if (iconWidget != null && iconWidget instanceof FocusWidget)
							((FocusWidget) iconWidget).setEnabled(((HasMenuItem)data).isEnable());
						return iconWidget;
					}
					@Override
					public int colSpan(MenuItemBase data) {
						if (data instanceof HasMenuItemSeperator || data instanceof HasMenuItemTitle){
							return 3;
						}
						return super.colSpan(data);
					};
					@Override
					public HorizontalAlignmentConstant alignment(MenuItemBase data) {
						if (data instanceof HasMenuItemTitle)
							return HasHorizontalAlignment.ALIGN_LEFT;
						return HasHorizontalAlignment.ALIGN_CENTER;
					}
				});

				add(new ReadOnlyTextColWidget<MenuItemBase>("") {
					
					@Override
					public TextDesc createDisplayText(MenuItemBase data) {
						HasMenuItem menuItem = (HasMenuItem)data;
						String detailText = (menuItem instanceof HasMenuItemDetailText)?((HasMenuItemDetailText)menuItem).getDetailText(): null;
						String txt = menuItem.getDisplayText() ;
						boolean menuItemEnable = menuItem.isEnable(); 
						if (detailText != null && !detailText.trim().equals(""))
							txt += "</BR><span class='menuDetailText " + (menuItemEnable?"":"menuItemDisable") + "'>" + detailText +"</span>";
//							txt += menuItemEnable?"</BR><span class='menuDetailText'>" + menuItem.getDetailText() +"</span>":"</BR>" + menuItem.getDetailText();
						if (!menuItemEnable){
							txt = "<span class ='menuItemDisable'>" + txt + "</span>";
						}
						return new TextDesc(true, txt);
					}
					public String title(MenuItemBase data) {
//						HasMenuItem menuItem = (HasMenuItem)data;
						return (data instanceof HasMenuItemToolTip)?((HasMenuItemToolTip)data).getToolTip(): null;
					};
				});
				add(new SmartUserManagedImageColWidget<HasMenuItem>("") {
					
					@Override
					public String fetchImageUrl(HasMenuItem data) {
						if (!(data instanceof HasMenuItemSubMenu))
							return null; 
						SubMenuItemBase subMenuItems = ((HasMenuItemSubMenu)data).getSubMenu();
//						List<? extends MenuItemBase> subMenu = data.getSubMenu();
						boolean hasSubMenu = !(subMenuItems==null);
						if (hasSubMenu)
							return data.isEnable()? "images/mint/arrow_right_grey.png": "images/mint/arrow_right_spearmint.png"; 
						else 
							return null;
					}
				});
				
			}
		});
	}
//	private WidgetPopupMenuBase subMenuWidget; 
	boolean autoFit = true; //This field should have the same value as MenuBase.autoFit
//	private class WidgetPopupMenuBase extends WidgetPopupMenu{
//		int top, left;
//		@Override
//		public int getMenuLeft() {
//			return left;
//		}
//
//		@Override
//		public int getMenuTop() {
//			return top;
//		}
//		
//	}

//	private MenuItemBase hilitedMenuItem = null;
	
	@Override
	public boolean onBeforeRowHilite(MenuItemBase data) {
		return data instanceof HasMenuItem && ((HasMenuItem)data).isEnable();
	}
	@Override
	public boolean onBeforeCellHilite(MenuItemBase data, ColWidget colWidget) {
		return false;
	}

//	public void setAutoFit(boolean autoFit) {
//		this.autoFit = autoFit; 
//		if (subMenu != null)
//			subMenu.setAutoFit(autoFit);
//	}
	private HasMenuItem emptyMenuItem = new MenuItem(" (Empty) ") {
		{
			setEnable(false);
			setIcon(new Image("images/silk/bin_empty.png"));
		}

		@Override
		public boolean onSelect() {
			return false;
		}
	};
	public void renderTab(List<? extends MenuItemBase> data) {
		System.out.println("rendering tab --");
		if (data == null || data.size() == 0){
			render(0, emptyMenuItem);
			removeRows(1);
		}else {
		int dataSize = data.size();
		for (int i = 0; i < dataSize; i++)
			render(i, data.get(i));
		removeRows(dataSize);
		}
	
	}
	@Override
	public boolean isAlternateRowColor() {
		return false; 
	}
}
